package com.city.ajax.house;

/*
 * @Author: 李顺时
 * @Description:
 * @Date: Create in 9:25 2018/7/29
 * @Modified By:
 * */


import com.city.domain.pub.PubHouseinfo;
import com.city.domain.pub.PubUser;
import com.city.service.pub.PubHouseinfoService;
import com.city.service.pub.PubUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.HashMap;

import java.util.Map;


@RequestMapping("/ajax/searchHouseInfo")
@RestController
public class HouseInfoAjax {


    @Autowired
    private PubUserService pubUserService;

    @Autowired
    private PubHouseinfoService pubHouseinfoService;

    /*
     * @Author: 李顺时
     * @Description: 查看详细
     * @Date: 21:28 2018/7/28
     */
    @PostMapping("/searchHouseDetail")
    @ResponseBody
    public Map<String,Object> findPubHouseinfoId(@RequestParam(value = "id") Integer id)
    {
        Map<String,Object> map = new HashMap<String,Object>();

        map.put("data", pubHouseinfoService.getHouseinfo(id));
        map.put("res", "1");
        map.put("code", "成功");
        map.put("count",1000);
        return map;
    }


    @PostMapping("/searchHousePosition")
    @ResponseBody
    public Map<String,Object> findPubHousePosition()
    {
        Map<String,Object> map = new HashMap<String,Object>();
        System.out.println(pubHouseinfoService.getHouseinfoGIS());
        map.put("data", pubHouseinfoService.getHouseinfoGIS());
        map.put("res", "1");
        map.put("code", "成功");
        map.put("count",1000);
        return map;
    }


    /**
     * @Author: 李顺时
     * @Description: gis以房找人搜索
     * @Date: 16:43 2018/8/24
     */
    @PostMapping("/searchHousePositionGisByName")
    @ResponseBody
    public Map<String,Object> findPubHousePositionGis(@RequestParam(value = "keyword") String keyword)
    {

        Map<String,Object> map = new HashMap<String,Object>();
        Integer count = 0;// 查询到房子的总记录数
        List<PubUser> user = pubUserService.getUserByName(keyword);//从pub_user表中查找满足条件的人的household_id
        List<PubHouseinfo> houseinfos = new ArrayList<>();//需要回传的搜索到的房子数据
        for (PubUser u: user) {
            PubHouseinfo house = pubHouseinfoService.getHouseinfoPostion(u.getHouseholdId());
            if(house != null && !houseinfos.contains(house)){
                count ++;
                houseinfos.add(house);
            }
        }

        map.put("data", houseinfos);
        map.put("res", "1");
        map.put("code", "成功");
        map.put("count", count);
        return map;
    }


}
